package com.huika.renrench.utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;

import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.hk.app.android.lib.http.VolleyRequestManager;
import com.huika.lib.ui.widget.ToastMsg;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.http.ServerFlagError;
import com.huika.renrench.http.ServerJsonUnParseError;
import com.huika.renrench.support.bean.UserModel;
import com.huika.renrench.support.bean.WebNetBean;

public class WebNetUtils {
	public static Context context;
	private static ArrayList<WebNetBean> webNetBeans;
	private static String thistype = "";
	
	public WebNetUtils() {
		super();
//		this.context = context;
	}

	public void webNet() {
		UserModel userModel = GlobalApp.userModel;
		FormResultRequest<ArrayList<WebNetBean>> request = new FormResultRequest<ArrayList<WebNetBean>>(UrlConstants.GETWEBURLLIST, webListener, errorListener,
				new TypeToken<RequestResult<ArrayList<WebNetBean>>>() {
				}.getType());
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putStringTypeParam("userId", userModel != null ? userModel.getUserId() : "0");
		request.setRequestParams(ajaxParams);
		VolleyRequestManager.addRequest(request, this);
	}

	private Listener<RequestResult<ArrayList<WebNetBean>>> webListener = new Listener<RequestResult<ArrayList<WebNetBean>>>() {

		@Override
		public void onResponse(RequestResult<ArrayList<WebNetBean>> arg0) {
			if (arg0.flag == 1 && arg0.getRs() != null) {
				webNetBeans = arg0.getRs();
			}
		}
	};

	private ErrorListener errorListener = new ErrorListener() {
		@Override
		public void onErrorResponse(VolleyError error) {
			String msg = "网络异常";
			if (error instanceof ServerFlagError) {
				msg = ((ServerFlagError) error).result.msg;
			} else if (error instanceof ServerJsonUnParseError) {
				try {
					String res = ((ServerJsonUnParseError) error).result;
					JSONObject jsonObject = new JSONObject(res);
					msg = jsonObject.getString("msg");
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else {
				try {
					if (error.networkResponse != null && error.networkResponse.data != null) {
						msg = new String(error.networkResponse.data, "utf-8");
						if (TextUtils.isEmpty(msg)) {
							msg = "未知异常";
						}
					}
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
			}
			if (thistype!=null&&thistype.equals("12")) {
				 ToastMsg.showToastMsg(context,msg);
			}
		}
	};
	
	public static String getWebUrl(String type){
		thistype = type;
		if(webNetBeans == null) {//modify by zc 当第一次进入应用时候为空，点击会再请求网页接口，目前暂时不能点击马上跳到返回的页面，需要再返回点一次能进入网页，后期优化
			new WebNetUtils().webNet();
			return "";
		}
		//1.充值2.提现3.本息保障4.我要借款5.店铺装修6.常见问题7.关于人人创8.服务协议9.投资记录10. 投资管理11. 借款管理细则
		for (int i = 0; i < webNetBeans.size(); i++) {
			if (type.equals(webNetBeans.get(i).getType())) {
				if(webNetBeans.get(i).getWebUrl().endsWith("userId=")) {//带有需要登录的时候加上userId 
					return (webNetBeans.get(i).getWebUrl() + GlobalApp.getInstance().getUserId());
				}
				return webNetBeans.get(i).getWebUrl();
			}
		}
		return "";
	}
}
